/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Settings.java
 *
 * Created on Jul 4, 2012, 10:39:20 AM
 */

package com.apps.ui;

import com.apps.beans.AccountBean;
import com.apps.beans.EnvironmentBean;
import com.apps.beans.SettingsBean;
import com.apps.controllers.AccountSetupManager;
import com.apps.utils.StartupHelper;
import java.util.ArrayList;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

/**
 *
 * @author ARIJIT PAL
 */
public class Settings extends javax.swing.JFrame {

    private ArrayList<EnvironmentBean> envArrList;
    private String startupTiming;
    /** Creates new form Settings */
    public Settings() throws Exception {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        envSetupOption = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        sysTray = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        envList = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        pathText = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        openDir = new javax.swing.JCheckBox();
        setupBtn = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        cancelBtn = new javax.swing.JButton();
        saveBtn = new javax.swing.JButton();

        setAlwaysOnTop(true);
        setLocationByPlatform(true);
        setResizable(false);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel11.setText("Application Settings:");

        sysTray.setText("Close the application window to system tray");

        jLabel1.setText("Current Environment Name: ");

        envList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Environment" }));
        envList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                envListsetEnvInfo(evt);
            }
        });

        jLabel2.setText("Save Result Path: ");

        jButton1.setText("...");

        openDir.setText("Open the result folder after the search");

        setupBtn.setText("Setup Now");
        setupBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setupBtnActionPerformed(evt);
            }
        });

        envSetupOption.add(jRadioButton1);
        jRadioButton1.setText("Check for environment setup before each search (NOT Recomended)");
        jRadioButton1.setName("1"); // NOI18N
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        envSetupOption.add(jRadioButton2);
        jRadioButton2.setText("Check for environment setup on startup");
        jRadioButton2.setName("2"); // NOI18N
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        envSetupOption.add(jRadioButton3);
        jRadioButton3.setText("Do NOT check for environment setup");
        jRadioButton3.setName("3"); // NOI18N
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        cancelBtn.setText("Cancel");

        saveBtn.setText("Save");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sysTray)
                    .addComponent(jLabel11)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(envList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(setupBtn))
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton1)
                    .addComponent(openDir)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pathText, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(saveBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelBtn)
                .addGap(122, 122, 122))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addComponent(sysTray)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(envList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(setupBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pathText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(openDir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn)
                    .addComponent(saveBtn))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void envListsetEnvInfo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_envListsetEnvInfo
        // TODO add your handling code here:
        
}//GEN-LAST:event_envListsetEnvInfo

    private void setupBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setupBtnActionPerformed
        // TODO add your handling code here:
        EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
        if (env != null) {
            try {
                ArrayList<AccountBean> accList = AccountSetupManager.getAccList(env.getCode());
                StartupHelper.closeAllShell();
                if (accList.size() > 0) {
                    StartupHelper.openAllShell(accList);
                    StartupHelper.getStatusChecker().setRefreshFlag(true);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_setupBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        // TODO add your handling code here:
        //setup first
        EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
        /*if (env != null) {
            try {
                ArrayList<AccountBean> accList = AccountSetupManager.getAccList(env.getCode());
                StartupHelper.closeAllShell();
                if (accList.size() > 0) {
                    StartupHelper.openAllShell(accList);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }*/
        //
        SettingsBean bean = new SettingsBean();
        bean.setApps_system_tray(sysTray.isSelected()?"true":"false");
        bean.setApps_env_code(env!=null?env.getCode():"");
        bean.setApps_env_setup_timing(startupTiming);
        bean.setApps_result_path(pathText.getText());
        bean.setApps_result_opendir(openDir.isSelected()?"true":"false");
        StartupHelper.setSettings(bean);
    }//GEN-LAST:event_saveBtnActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        startupTiming = "1";
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        startupTiming = "2";
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        // TODO add your handling code here:
        startupTiming = "3";
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    //initial setup for the UI
    public void init() throws Exception {
        //init settings
        SettingsBean settings = StartupHelper.getSettings();

        if(settings.getApps_system_tray().equalsIgnoreCase("true"))
            sysTray.setSelected(true);

        envArrList = AccountSetupManager.getEnvList();
        envList.removeAllItems();
        int count = 0;
        for (EnvironmentBean env : envArrList) {
            envList.addItem(env);
            if(settings.getApps_env_code().equalsIgnoreCase(env.getCode()))
                envList.setSelectedIndex(count);
            else
                count++;
        }

        startupTiming = settings.getApps_env_setup_timing();
        if(startupTiming.equalsIgnoreCase("1"))
            jRadioButton1.setSelected(true);
        else if(startupTiming.equalsIgnoreCase("2"))
            jRadioButton2.setSelected(true);
        else if(startupTiming.equalsIgnoreCase("3"))
            jRadioButton3.setSelected(true);

        pathText.setText(settings.getApps_result_path());

        if(settings.getApps_result_opendir().equalsIgnoreCase("true"))
            openDir.setSelected(true);
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]){
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    new Settings().setVisible(true);
                }catch(Exception ex){ ex.printStackTrace();}
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JComboBox envList;
    private javax.swing.ButtonGroup envSetupOption;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JCheckBox openDir;
    private javax.swing.JTextField pathText;
    private javax.swing.JButton saveBtn;
    private javax.swing.JButton setupBtn;
    private javax.swing.JCheckBox sysTray;
    // End of variables declaration//GEN-END:variables

}
